package com.chenzhou.controller;

import com.chenzhou.entity.GoodsEntity;
import com.chenzhou.entity.GoodsTypeEntity;
import com.chenzhou.service.GoodsService;
import com.chenzhou.service.GoodsTypeServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService service;

    @Autowired
    private GoodsTypeServie goodsTypeServie;

    @RequestMapping("/queryAllGoods")
    public String queryAllGoods(HttpServletRequest request){
        List<GoodsEntity> list = service.queryAllGoodsEntity();
        request.setAttribute("glist",list);
        request.setAttribute("menu","goods");
        return "goods/show_goods";
    }

    @RequestMapping("/queryGoodsByID")
    public String queryGoodsByID(HttpServletRequest request,Integer id){
        GoodsEntity goodsEntity = service.queryGoodsEntityOne(id);
        List<GoodsTypeEntity> list = goodsTypeServie.queryAllGoodsType();
        request.setAttribute("gtlist",list);
        request.setAttribute("goods",goodsEntity);
        request.setAttribute("menu","goods");
        return "goods/modify_goods";
    }


    @RequestMapping("/goto_add_goods")
    public String add_goodsByType(HttpServletRequest request){
        List<GoodsTypeEntity> list = goodsTypeServie.queryAllGoodsType();
        request.setAttribute("gtlist",list);
        request.setAttribute("menu","goods");
        return "goods/add_goods";
    }

    @RequestMapping("/add_Goods")
    public String add_Goods(HttpServletRequest request,GoodsEntity goodsEntity){
        int a = service.add_GoodsEntity(goodsEntity);
        if(a>0) {
            return "redirect:/goods/queryAllGoods";
        }else {
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/modify_Goods")
    public String modify_Goods(HttpServletRequest request,GoodsEntity goodsEntity){
        System.out.println("进入控制器！！！！");
        int a = service.modify_GoodsEntity(goodsEntity);
        if(a>0) {
            return "redirect:/goods/queryAllGoods";
        }else {
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/delete_Goods")
    public String delete_Goods(HttpServletRequest request,Integer id){
        int a = service.delete_GoodsEntity(id);
        if(a>0) {
            return "redirect:/goods/queryAllGoods";
        }else {
            return "forward:/error.jsp";
        }
    }
}
